<?php

class Core_Pack 
{
	/**
	 * suma pól
	 * @var int
	 */
	public $area = 0;
	
	/**
	 * waga
	 * @var int
	 */
	public $weight = 0;
	
	/**
	 * pusta przestrzeń jaka musi zostać w pudełkach w %
	 * @var int
	 */
	public $empty = 20;
	
	/**
	 * wynik
	 * 
	 * @var int
	 */
	public $result = 0;
	
	/**
	 * area - pole powierzchni w milimetrach
	 * weight - dopuszczalana waga dla pudełka w gramach
	 * price - cena za pudełko
	 * price_for_overweight - cena za przekroczenie wagi
	 * overweight - co ile gram naliczać stawkę za przekroczenie
	 * 
	 * @var array
	 */
	public $items;
	
	/**
	 * kostruktor
	 * 
	 * @param int $area
	 * @param int $weight
	 * @return void
	 */
	public function __construct( $area, $weight ) {
		$this->area = $area;
		$this->weight = $weight;
		
		$Pack = new Pack();
		$this->items = $Pack->getListService()->toArray();
	}
	
	/**
	 * funkcja oblicza jaki i ile pudełek potrzeba do spakowania
	 * 
	 * @return array
	 */
	public function calc() {
		$last;
		$price2 = 0;
		//Core_Debug::pr($value['area']);
		
		foreach( $this->items as $value ) {
			$pri = unserialize($value['price']);
			//Core_Debug::pr($value['area']);
			
			if( $value['area'] > $this->area ) {
				//$this->area = 0;
				$price = unserialize($value['price']);
				
				$a = 0;
				$b = 0;
				
				for($i=0;$i < count($price);$i=+6)
				{
					$a = $price[$i+1];	// waga od
						
					$b = $price[$i+3];	//waga do

					
					if ($a > $b)
					{
						$tmp = $b;
						$b = $a;
						$a = $tmp;
					}
					
					if(($this->weight) >= $a && ($this->weight) < $b)
					{
						$price2 = $price[$i+5];	//cena za pudelko
						$this->result = $this->result + $price2;
						//$this->result = $this->result + $value['price'];
						return $this->result;
						break;
					}
	
				}
				
			}

			$last = $value;
		}
		
		$price = unserialize($last['price']);
		
		if( $this->area > 0 ) {
			$this->area = $this->area - $last['area'];
			$this->result = $this->result + $price2;
			$this->area2 = $this->area + $last['area'];
			$this->result = $this->result + $price[5];
			//Core_Debug::pr($this->area);
			$this->calc();
		}
		//Core_Debug::pr($value['area']);
		//Core_Debug::pr($this->area);

	}
}